import subprocess,re
def check(*pkgs):
    ret = subprocess.Popen(["python", "-m", "pip", "list"],stdout=subprocess.PIPE).communicate()
    installed = [str(x, "utf-8").split()[0] for x in ret[0].split(b"\n") if(re.search(r".[\d\.]+", str(x, "utf-8")))]
    for pkg in pkgs:
        if(pkg not in installed): 
            print("install {}".format(pkg))
            ret = subprocess.Popen(["python","-m","pip","install", pkg])
            ret.wait()

def install(*pkgs):
    ret = subprocess.Popen(["apt", "list", "--installed"],stdout=subprocess.PIPE).communicate()
    installed = [str(x, "utf-8").split('/')[0] for x in ret[0].split(b"\n")]
    for pkg in pkgs:
        if(pkg not in installed): 
            print("install {}".format(pkg))
            ret = subprocess.Popen(['apt', 'install', '-y', pkg])
            ret.wait()

check("Pillow", "SimpleWebSocketServer", "mariadb", "apscheduler", "requests", "SQLAlchemy", "bs4",\
         "mysqlclient", "lxml", "selenium", "pexpect", "wexpect", "tzlocal", "pysbd", "urllib3",\
         "filetype", "rarfile", "modelscope","openai", "transformers", "Flask", "SQLAlchemy",\
         "chromadb", "colorama", "py7zr", "playwright")
install("jp2a")
